﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_Sua_bai_viet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPostCategories();
            GetPostDetailByPostID();
        }
    }
    protected void GetPostDetailByPostID()
    {
        //try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetPostDetailByPostID";
            Dal.ParameterCollection = new string[1] { "@BaiVietID"};
            Dal.ValueCollection = new string[1] { Request.QueryString["BaiVietID"].ToString()};
            DataRow dr = Dal.GetDataRow();
            ddlPostsType.SelectedValue = dr["ChuDeID"].ToString();
            txtPostsTitle.Text = dr["TieuDe"].ToString();
            if (dr["IsActive"].ToString() == "true")
                rdbActive.SelectedValue = "1";
            else
                rdbActive.SelectedValue = "0";
            txtDescription.Text = dr["MoTa"].ToString();
            CKEditorControl1.Text = dr["NoiDung"].ToString();
        //}
        //catch { }
    }
    protected void GetPostCategories()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetAllPostscategories";
        DataTable db = Dal.GetDataTable();
        ddlPostsType.DataSource = db;
        ddlPostsType.DataTextField = db.Columns["TenChuDe"].ToString();
        ddlPostsType.DataValueField = db.Columns["ChuDeID"].ToString();
        ddlPostsType.DataBind();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        try {
            if (!fuAvatar.HasFile)
            {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdatePostsContentWithNoneImage";
                Dal.ParameterCollection = new string[6] { "@BaiVietID", "@TieuDe", "@MoTa", "@NoiDung", "@ChuDeID", "@IsActive" };
                Dal.ValueCollection = new string[6] { Request.QueryString["BaiVietID"].ToString(), txtPostsTitle.Text, txtDescription.Text, CKEditorControl1.Text, ddlPostsType.SelectedValue, rdbActive.SelectedValue };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                lblInfo.Text = "Cập nhật thành công !";
            }
            else
            {
                string filename = Path.GetFileName(fuAvatar.FileName);
                fuAvatar.SaveAs(Server.MapPath("~/Images/thumb-news/" + filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdatePostsContentWithImage";
                Dal.ParameterCollection = new string[7] { "@BaiVietID", "@TieuDe", "@MoTa", "@NoiDung","@HinhDaiDien", "@ChuDeID", "@IsActive" };
                Dal.ValueCollection = new string[7] { Request.QueryString["BaiVietID"].ToString(), txtPostsTitle.Text, txtDescription.Text, CKEditorControl1.Text, "Images/thumb-news/"+filename, ddlPostsType.SelectedValue, rdbActive.SelectedValue };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                lblInfo.Text = "Cập nhật thành công !";
            }
        }
        catch { }
    }
}
